package com.hbt.orderfood.fragment;


import android.content.DialogInterface;
import android.graphics.Point;
import android.os.Bundle;

import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import com.hbt.orderfood.R;
import com.hbt.orderfood.activity.ManagerActivity;
import com.hbt.orderfood.adapter.MngFoodAdapter;
import com.hbt.orderfood.db.DBAdapter;
import com.hbt.orderfood.entitys.Food;
import com.hbt.orderfood.utils.StringUtils;
import com.noober.menu.FloatMenu;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class FoodFragment extends Fragment {

    private  List<Food> foods;
    private RecyclerView recycle;
    private MngFoodAdapter mngFoodAdapter;
    String type;

    public void setPoint(Point point) {
        this.point = point;
    }

    private Point point;

    public FoodFragment(String type) {
        // Required empty public constructor
        this.type = type;
    }
    //初始化列表
    void initRecycler(RecyclerView recycle){
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycle.setLayoutManager(layoutManager);
        mngFoodAdapter = new MngFoodAdapter(foods);
        mngFoodAdapter.setClickListener(new MngFoodAdapter.ItemClickListener() {
            @Override
            public void itemClick(Food food) {
                final FloatMenu floatMenu = new FloatMenu(getContext(), recycle);
                floatMenu.items("修改", "删除");
                //设置食物列表的点击监听事件
                floatMenu.setOnItemClickListener(new FloatMenu.OnItemClickListener() {
                    @Override
                    public void onClick(View v, int position) {
                        if(position == 0){
                            //修改（弹出修改弹窗）
                            createDialogAndUpdateFoodByFood_id(food);
                        }else{
                            //删除
                            createDialogAndDelFood(food.getId());
                        }
                    }
                });
                floatMenu.show(point);
            }
        });
        recycle.setAdapter(mngFoodAdapter);
    }


    //弹出窗口并删除食物项
    void createDialogAndDelFood(int food_id){
        AlertDialog dialog = new AlertDialog.Builder(getActivity())
                .setTitle("删除提醒")
                .setPositiveButton("删除", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        DBAdapter dbAdapter = new DBAdapter(getActivity());
                        if (dbAdapter.hasFoodidInOrders(food_id)){
                            AlertDialog dialog_inner = new AlertDialog.Builder(getActivity())
                                    .setTitle("删除提醒").setMessage("请先确认订单").create();
                            dialog_inner.show();
                        }else {
                            dbAdapter.deleteFoodByFoodID(food_id);
                            mngFoodAdapter.setFoods(dbAdapter.getFoodsByType(type));
                            mngFoodAdapter.notifyDataSetChanged();
                        }

                    }
                })
                .setMessage("确定删除该项吗")
                .create();
        dialog.show();
    }//createDialogAndDelFood

    //弹出弹窗并修改通过food_id修改food
    void createDialogAndUpdateFoodByFood_id(Food food){
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        View v = inflater.inflate(R.layout.update_food, null);
        EditText food_name = v.findViewById(R.id.food_name);
        EditText food_price = v.findViewById(R.id.food_price);
        food_name.setText(food.getName());
        food_price.setText(String.valueOf(food.getPrice()));
        AlertDialog dialog = new AlertDialog.Builder(getActivity())
                .setTitle("修改报刊")
                .setPositiveButton("修改", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String name = food_name.getText().toString();
                        int price = Integer.parseInt(food_price.getText().toString());
//                        Food food_new = new Food();
                        food.setName(name);
                        food.setPrice(price);
                        DBAdapter dbAdapter = new DBAdapter(getActivity());
                        dbAdapter.updateFoodByFoodID(String.valueOf(food.getId()), food);
                        mngFoodAdapter.setFoods(dbAdapter.getFoodsByType(type));
                        mngFoodAdapter.notifyDataSetChanged();
                    }
                })
                .setView(v)
                .create();
        dialog.show();
    }


    //更新数据
    public void updateData(){
        foods = new DBAdapter(getContext()).getFoodsByType(type);
        mngFoodAdapter.setFoods(foods);
        mngFoodAdapter.notifyDataSetChanged();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_food, container, false);
        foods = new DBAdapter(getActivity()).getFoodsByType(type);
        recycle = view.findViewById(R.id.recycle);
        initRecycler(recycle);
        System.out.println("切换页面");
        return view;
    }

}
